﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using ExaminationSys.DataAccess.Entity;
using DapperExtensions;

namespace ExaminationSys.DataAccess.Repository
{
    public  class SectionInfoRepository : Repository<SectionInfo>
    {
        public SectionInfoRepository(SqlConnection context)
            : base(context)
        {
            db = context;
        }

        private SqlConnection db;

        public override SectionInfo GetById(dynamic id)
        {
            return base.ObjectSet.Get<SectionInfo>(db, id, null, null);
        }
    }

    public class SectionInfoService
    {
        private SectionInfoService()
        {
        }

        public List<SectionInfo> GetAll()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SectionInfo.GetList();
                return result.ToList();
            }
        }

        public List<SectionInfo> GetPublishList()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var pg = new PredicateGroup {Operator = GroupOperator.And, Predicates = new List<IPredicate>()};
                pg.Predicates.Add(Predicates.Field<SectionInfo>(f => f.Status, Operator.Eq, 1));
                var result = work.SectionInfo.GetList(pg);
              
                return result.ToList();
            }
        }

        public List<SectionInfo> GetStudySectionList()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<SectionInfo>(f => f.SectionTypeID, Operator.Eq, 1));
                var result = work.SectionInfo.GetList(pg);
                return result.ToList();
            }
        }


        public List<SectionInfo> GetListByPage(string sqlwhere, string orderbyFiled, int pagesize, int pageindex, out int totalPage, out int totalRecord)
        {
            if (string.IsNullOrWhiteSpace(orderbyFiled))
                throw new ArgumentNullException("orderbyFiled");

            if (string.IsNullOrWhiteSpace(sqlwhere))
                sqlwhere = " 1=1";
            using (var db = DataContent.DBServer())
            {
                var obj = new
                {
                    TableName = "SectionInfo",
                    Fields = "*",
                    OrderField = orderbyFiled,
                    sqlWhere = sqlwhere,
                    pageSize = pagesize,
                    pageIndex = pageindex,
                };

                var dynamicParam = new DynamicParameters(obj);
                dynamicParam.Add("TotalPage", null, DbType.Int32, ParameterDirection.Output, 32);
                dynamicParam.Add("TotalRecord", 0, DbType.Int32, ParameterDirection.Output, 32);

                var items = db.Query<SectionInfo>("PagingRecord", dynamicParam, null, true, null, CommandType.StoredProcedure);
                totalPage = dynamicParam.Get<int>("TotalPage");
                totalRecord = dynamicParam.Get<int>("TotalRecord");
                return items.ToList();
            }
        }

        public List<View_TableCount> GetTableCount()
        {
            using (var db = DataContent.DBServer())
            {
                var obj = new
                {
                    TableName = "View_TableCount",
                    Fields = "*",
                    OrderField = "SortOrder",
                    sqlWhere = "",
                    pageSize = 100,
                    pageIndex = 1,
                };

                var dynamicParam = new DynamicParameters(obj);
                dynamicParam.Add("TotalPage", 0, DbType.Int32, ParameterDirection.Output, 32);
                dynamicParam.Add("TotalRecord", 0, DbType.Int32, ParameterDirection.Output, 32);
                var items = db.Query<View_TableCount>("PagingRecord", dynamicParam, null, true, null, CommandType.StoredProcedure);

                return items.ToList();
            }
        }

        public bool CheckNameExists(string name, Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<SectionInfo>(f => f.Name, Operator.Eq, name));
                pg.Predicates.Add(Predicates.Field<SectionInfo>(f => f.ID, Operator.Eq, id, true));
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SectionInfo.GetList(pg).FirstOrDefault();
                return result != null;
            }
        }
        public SectionInfo Get(Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.SectionInfo.GetById(id);
                return result;
            }
        }
        public bool Update(SectionInfo entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.SectionInfo.Update(entity);
            }
        }
        public dynamic Insert(SectionInfo entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.SectionInfo.Insert(entity, null);
            }
        }

        public bool Delete(List<string> ids)
        {
            if (ids == null || ids.Count == 0) return false;
            bool flag = false;
            using (var db = DataContent.DBServer())
            {
                int retCode = 0;
                foreach (var id in ids)
                {
                    retCode = db.Execute(@"update SectionInfo set Status=0 where ID=@TmpId",
                                         new[] { new { TmpId = id } });
                    if (retCode <= 0)
                    {
                        break;

                    }
                }
                flag = true;
            }
            return flag;
        }
    }
}